using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class 调酒系统: MonoBehaviour
{
    [Header("酒")]
    [SerializeField] private List<WineSO> wineList = new List<WineSO>();
    private WineSO wine;
    [SerializeField] private bool isPassed = false;
    public bool IsPassed { get => isPassed; set => isPassed = value; }
    [Header("调酒按钮")]
    public Button[] ingredientBtn = new Button[5];
    private int[] ingredientNum = new int[5];
    [SerializeField] private bool isIced = false;
    [SerializeField] private bool isTimed = false;
    [Header("加冰")] public Button iceBtn;
    [Header("陈化")] public Button timeBtn;
    [Header("重做")] public Button clearBtn;
    [Header("上酒")] public Button confirmBtn;

    [Header("调料台")] public IngredientPanel[] ingredientPanel = new IngredientPanel[5];
    [Header("加冰面板")] public GameObject icePanel;
    [Header("陈化面板")] public GameObject timePanel;
    [Header("显示组件")]
    [Header("图片")] public Image icon;
    [Header("酒名")] public TextMeshProUGUI wineName;
    [Header("描述")] public TextMeshProUGUI wineDescription;
    private void Awake()
    {
        // Correct array initialization
        ingredientNum = new int[5]{0,0,0,0,0};
        
        // Initialize ( panelsoptional but good practice)
        for (int i = 0; i < ingredientPanel.Length; i++)
        {
            ingredientPanel[i].SetNum(ingredientNum[i]);
        }
        // 按钮初始化和事件绑定
        for (int i = 0; i < ingredientBtn.Length; i++)
        {
            if (ingredientBtn[i] != null)
            {
                int index = i; // 捕获当前索引
                ingredientBtn[i].onClick.AddListener(() => IngredientAdd(index));
            }
            else
            {
                Debug.LogWarning("ingredientBtn[" + i + "]未引用按钮！");
            }
        }
        
        iceBtn.onClick.AddListener(AddIce);
        timeBtn.onClick.AddListener(AgeCocktail);
        clearBtn.onClick.AddListener(ClearIngredient);
        confirmBtn.onClick.AddListener(ConfirmCocktail);
    }

    private void Update()
    {
        icon.sprite = wine.icon;
        wineName.text = wine.name;
        wineDescription.text = wine.itemDescription;
        
        
    }

    public void SetWine(string name)
    {
        foreach (var VARIABLE in wineList)
        {
            if (VARIABLE.name == name)
            {
                wine = VARIABLE;
                isPassed = wine.isPassed;
                return;
            }
        }
        Debug.LogError($"{name} is not found in Wine List");
    }
    // Add button index as parameter
    private void IngredientAdd(int index)
    {
        Debug.Log($"IngredientAdd{index}");
        ingredientNum[index]++;
        ingredientPanel[index].SetNum(ingredientNum[index]);
    }

    public void AddIce()
    {
        isIced = !isIced;
        // Implementation for adding ice
        icePanel.SetActive(true);
    }

    public void AgeCocktail()
    {
        isTimed = !isTimed;
        // Implementation for aging
        timePanel.SetActive(true);
    }

    public void ClearIngredient()
    {
        ingredientNum = new int[5]; // Correct array reset
        foreach (var panel in ingredientPanel)
        {
            panel.SetNum(0);
        }
    }

    public void ConfirmCocktail()
    {
        // Check recipe logic here
        bool isCorrectRecipe = CheckRecipe();
        isPassed = isCorrectRecipe;
        wine.isPassed = isPassed;
        
        if (isCorrectRecipe)
        {
            Debug.Log("调酒成功");
            // Serve cocktail logic
            ClearIngredient();
            return;
        }
        Debug.Log("调酒失败");
        ClearIngredient();
    }

    private bool CheckRecipe()
    {
        // Compare with WineSO recipe
        if (wine == null) return false;
        for (int i = 0; i < ingredientNum.Length; i++)
        {
            if (ingredientNum[i] != wine.ingredientDemand[i])
            {
                return false;
            }
        }

        if (isIced != wine.isIce || isTimed != wine.isTime)
        {
            return false;
        }
        
        return true;
    }

    // Example of panel management
    public void ClosePanels()
    {
        icePanel.SetActive(false);
        timePanel.SetActive(false);
    }
}
    
    
    
    

